package com.itlong.cloud.controller.inspect;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.itlong.cloud.POJO.DTO.property.PropertyInspectPlanTimeDTO;
import com.itlong.cloud.POJO.DTO.property.PropertyInspectSettingDTO;
import com.itlong.cloud.POJO.VO.MessageVO;
import com.itlong.cloud.POJO.VO.property.PropertyInspectSettingVO;
import com.itlong.cloud.abstracts.BaseController;
import com.itlong.cloud.annotation.AuthAccessRequired;
import com.itlong.cloud.annotation.LoggerInfo;
import com.itlong.cloud.constants.LogDescConstant;
import com.itlong.cloud.enumerate.PropertyInspectTypeEnum;
import com.itlong.cloud.enumerate.ecode.BaseErrorCodeEnum;
import com.itlong.cloud.enumerate.ecode.PropertyErrorCodeEnum;
import com.itlong.cloud.inspect.IPropertyInspectSettingServiceClient;
import com.itlong.cloud.thrown.DataAccessException;
import com.itlong.cloud.utils.network.http.HttpProtocolUtil;
import com.itlong.cloud.utils.page.Page;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <desc>
 *      物业—巡检管理-巡检设置模块。
 * <desc/>
 *
 * @createDate 2019-12-24
 */
@RestController
@RequestMapping("inspect/setting")
@RefreshScope
public class PropertyInspectSettingController extends BaseController {

    private static final Logger LOG = LoggerFactory.getLogger(PropertyInspectSettingController.class);

    @Autowired
    IPropertyInspectSettingServiceClient iPropertyInspectSettingServiceClient;


    /**
     * <desc>
     *      添加或修改巡检设置相关处理。
     * <desc/>
     *
     * @param  dto 巡检相关信息
     * @return
     * @author Juguang.S
     * @createDate 2019-12-24
     */
    @ResponseBody
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_INSPECT_POINT_SAVE_LOG_DESC)
    @AuthAccessRequired
    @RequestMapping(path = "/saveOrUpdateInspectInfo" , method = RequestMethod.POST)
    public Object saveInspectPoint(PropertyInspectSettingDTO dto) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectSettingDTO.class, request);
            if(null==dto || StringUtils.isEmpty(dto.getProjectId()) || StringUtils.isEmpty(dto.getType()+"")){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //添加校验名称
            if(StringUtils.isEmpty(dto.getInspectAreaId()) && StringUtils.isEmpty(dto.getInspectPointId()) &&
                    (StringUtils.isEmpty(dto.getInspectLineId()) && dto.getType()==3) && StringUtils.isEmpty(dto.getInspectPlanId())){
                if(iPropertyInspectSettingServiceClient.isExistByInspectName(dto)>0){
                    return new MessageVO(PropertyErrorCodeEnum.INSPECT_NAME_IS_EXIST_ERR.getErrorCode());
                }
            }else{
                //修改校验名称
                PropertyInspectSettingVO inspectSettingVO;
                if(iPropertyInspectSettingServiceClient.isExistByInspectName(dto)>0){
                    switch (PropertyInspectTypeEnum.getByEquipType(dto.getType())) {
                        case INSPECT_AREA:
                            inspectSettingVO = iPropertyInspectSettingServiceClient.getInspectNameById(dto);
                            if(!dto.getInspectAreaName().equals(inspectSettingVO.getInspectAreaName())){
                                return new MessageVO(PropertyErrorCodeEnum.INSPECT_NAME_IS_EXIST_ERR.getErrorCode());
                            }
                            break;
                        case INSPECT_POINT:
                            inspectSettingVO = iPropertyInspectSettingServiceClient.getInspectNameById(dto);
                            if(!dto.getInspectPointName().equals(inspectSettingVO.getInspectPointName())){
                                return new MessageVO(PropertyErrorCodeEnum.INSPECT_NAME_IS_EXIST_ERR.getErrorCode());
                            }
                            break;
                        case INSPECT_LINE:
                            inspectSettingVO = iPropertyInspectSettingServiceClient.getInspectNameById(dto);
                            if(!dto.getInspectLineName().equals(inspectSettingVO.getInspectLineName())){
                                return new MessageVO(PropertyErrorCodeEnum.INSPECT_NAME_IS_EXIST_ERR.getErrorCode());
                            }
                            break;
                        case INSPECT_PLAN:
                            inspectSettingVO = iPropertyInspectSettingServiceClient.getInspectNameById(dto);
                            if(!dto.getInspectPlanName().equals(inspectSettingVO.getInspectPlanName())){
                                return new MessageVO(PropertyErrorCodeEnum.INSPECT_NAME_IS_EXIST_ERR.getErrorCode());
                            }
                            break;
                    }
                }
            }
            if(dto.getType()==PropertyInspectTypeEnum.INSPECT_PLAN.getType()){
                List<PropertyInspectPlanTimeDTO> list  = JSON.parseArray(dto.getTimeStr(), PropertyInspectPlanTimeDTO.class);
                dto.setTimeArr(list);
            }
            iPropertyInspectSettingServiceClient.saveInspectPoint(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【物业_巡检管理-巡检设置模块_添加巡检点】失败",e);
        }
    }

    /**
     * <desc>
     *      获取巡检点编号 即蓝牙门禁deviceUnique。
     * <desc/>
     *
     * @param  dto 巡检点信息
     * @return 点编号集合
     * @author Juguang.S
     * @createDate 2019-12-24
     */
    @ResponseBody
    @RequestMapping(path = "/getInspectPointNumber" , method = RequestMethod.POST)
    public Object getInspectPointNumber(PropertyInspectSettingDTO dto) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectSettingDTO.class, request);
            if(null==dto || StringUtils.isEmpty(dto.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyInspectSettingServiceClient.getInspectPointNumber(dto));
        }catch (Exception e){
            throw new DataAccessException("【物业_巡检管理-巡检设置模块_获取巡检点编号】失败",e);
        }
    }

    /**
     * <desc>
     *      获取巡检设置相关分页列表。
     * <desc/>
     *
     * @param  dto 查询条件
     * @return 巡检分页集合
     * @author Juguang.S
     * @createDate 2019-12-24
     */
    @ResponseBody
    @AuthAccessRequired
    @PostMapping(path = "/getInspectInfoByPage")
    public Object getInspectInfoByPage(PropertyInspectSettingDTO dto) {
        try {
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectSettingDTO.class, request);
            if(null==dto || StringUtils.isEmpty(dto.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            Page<PropertyInspectSettingVO> page = iPropertyInspectSettingServiceClient.getInspectInfoByPage(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(), page);
        } catch (Exception e) {
            throw new DataAccessException("【物业_系统_设备管理】分页查询失败", e);
        }
    }

    /**
     * <desc>
     *      修改巡检设置相关状态。
     * <desc/>
     *
     * @param  dto 修改状态相关参数
     * @return
     * @author Juguang.S
     * @createDate 2019-12-24
     */
    @ResponseBody
    @LoggerInfo(operateDesc = LogDescConstant.PROPERTY_INSPECT_POINT_UPDATE_LOG_DESC)
    @AuthAccessRequired
    @RequestMapping(path = "/updateInspectSettingStatus" , method = RequestMethod.POST)
    public Object updateInspectSettingStatus(PropertyInspectSettingDTO dto) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectSettingDTO.class, request);
            if(null==dto || StringUtils.isEmpty(dto.getProjectId()) || StringUtils.isEmpty(dto.getInspectId()) ||
                    StringUtils.isEmpty(dto.getStatus()+"") || StringUtils.isEmpty(dto.getType()+"")){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            //删除巡检相关需校验是否与上级有所关联，有关联不允许删除
            if(dto.getStatus()==0 || dto.getStatus()==2){
                Integer mark = iPropertyInspectSettingServiceClient.isRelationByInspect(dto);
                //mark 1巡检区域有关联 2巡检点有关联 3巡检路线有关联
                if(mark==1){
                    return new MessageVO(PropertyErrorCodeEnum.INSPECT_AREA_IS_RELATION_ERR.getErrorCode());
                }else if(mark==2){
                    return new MessageVO(PropertyErrorCodeEnum.INSPECT_POINT_IS_RELATION_ERR.getErrorCode());
                }else if(mark==3){
                    return new MessageVO(PropertyErrorCodeEnum.INSPECT_LINE_IS_RELATION_ERR.getErrorCode());
                }
            }
            iPropertyInspectSettingServiceClient.updateInspectSettingStatus(dto);
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode());
        }catch (Exception e){
            throw new DataAccessException("【物业_巡检管理-巡检设置模块_修改巡检设置相关状态】失败",e);
        }
    }

    /**
     * <desc>
     *      获取巡检路线详情。
     * <desc/>
     *
     * @param  dto 巡检路线ID
     * @return 巡检路线详情
     * @author Juguang.S
     * @createDate 2019-12-26
     */
    @ResponseBody
    @RequestMapping(path = "/getInspectLineInfo" , method = RequestMethod.POST)
    public Object getInspectLineInfo(PropertyInspectSettingDTO dto) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectSettingDTO.class, request);
            if(null==dto || StringUtils.isEmpty(dto.getProjectId()) || StringUtils.isEmpty(dto.getInspectLineId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyInspectSettingServiceClient.getInspectLineInfo(dto));
        }catch (Exception e){
            throw new DataAccessException("【物业_巡检管理-巡检设置模块_获取巡检路线详情】失败",e);
        }
    }

    /**
     * <desc>
     *      获取巡检计划详情。
     * <desc/>
     *
     * @param  dto 巡检计划ID
     * @return 巡检计划详情
     * @author Juguang.S
     * @createDate 2019-12-27
     */
    @ResponseBody
    @RequestMapping(path = "/getInspectPlanInfo" , method = RequestMethod.POST)
    public Object getInspectPlanInfo(PropertyInspectSettingDTO dto) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectSettingDTO.class, request);
            if(null==dto || StringUtils.isEmpty(dto.getInspectPlanId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyInspectSettingServiceClient.getInspectLineInfo(dto));
        }catch (Exception e){
            throw new DataAccessException("【物业_巡检管理-巡检设置模块_获取巡检计划详情】失败",e);
        }
    }

    /**
     * <desc>
     *      获取巡检点列表。
     * <desc/>
     *
     * @param  dto 项目ID
     * @return 巡检点列表
     * @author Juguang.S
     * @createDate 2019-12-27
     */
    @ResponseBody
    @RequestMapping(path = "/getInspectPointList" , method = RequestMethod.POST)
    public Object getInspectPointList(PropertyInspectSettingDTO dto) {
        try{
            HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
            dto = HttpProtocolUtil.parseRequestParamToDTO(PropertyInspectSettingDTO.class, request);
            if(null==dto || StringUtils.isEmpty(dto.getProjectId())){
                return new MessageVO(BaseErrorCodeEnum.NOT_PARAM.getErrorCode());
            }
            return new MessageVO(BaseErrorCodeEnum.SUCCESS.getErrorCode(),iPropertyInspectSettingServiceClient.getInspectPointList(dto));
        }catch (Exception e){
            throw new DataAccessException("【物业_巡检管理-巡检设置模块_获取巡检点列表】失败",e);
        }
    }

}
